from app.service.ali import Ali
from app.service.tx import Tx
from app.service.hw import Hw
from app.functions import *
from app.config import SysConfig


def getDriverClass(name):
    drivers = {
        'aly': Ali,
        'txy': Tx,
        'hwy': Hw,
    }
    return drivers.get(name.lower())


if __name__ == '__main__':
    print("域名 API 调用开始")
    print("-".join(sys.argv))
    file_name, plat, action, certbot_domain, host, recordValue, ACCESS_KEY_ID, ACCESS_KEY_SECRET = sys.argv
    # # 获取类对象
    DriverClass = getDriverClass(plat)

    if not DriverClass:
        dd('Driver {} not found.'.format(plat))
    if not hasattr(DriverClass, 'getDomain'):
        dd("{} 缺少getDomain方法".format(plat))

    domainData = DriverClass.getDomain(certbot_domain)
    if domainData[0] == "":
        subDomain = host
    else:
        subDomain = host + "." + domainData[0]
    obj = DriverClass(ACCESS_KEY_ID, ACCESS_KEY_SECRET, domainData[1])

    if action == "add":
        res = obj.addRecord(subDomain, recordValue)
        if res.get('status') != SysConfig.STATUS_CODE_SUCCESS:
            dd("{} dns 记录添加失败-{}".format(plat, res.get('msg')))
    elif action == "clean":
        res = obj.cleanRecord(subDomain)
        if res.get('status') != SysConfig.STATUS_CODE_SUCCESS:
            dd("{} dns 记录清理失败-{}".format(plat, res.get('msg')))
    print("域名 API 调用结束")
